//显示默认文字
reg_msg = new Array();
reg_msg['username'] = new Array();
reg_msg['username']['normal'] = "4-16位字母或数字,不能有特殊符号";
reg_msg['password'] = new Array();
reg_msg['password']['normal'] = "4-24位. 只能包含数字, 字母和_";
reg_msg['password2'] = new Array();
reg_msg['password2']['normal'] = "请再次输入密码";
reg_msg['Email'] = new Array();
reg_msg['Email']['normal'] = "用于确认身份及找回密码,请正确填写";

window.onload = fm_ini;

    function fm_ini(){
        var fm, i, j;
        for(i = 0; i < document.forms.length; ++i){
            fm = document.forms[i];
            for(j = 0; j < fm.length; ++j){
                fm[j].onblur = function(){
                    if(tx_chk(oo(this.name))){
                        cancel_popup_win(this.name, false);
                    }
                }

                fm[j].onfocus = function(){
                    if(typeof reg_msg[this.name] != 'undiefined' && typeof reg_msg[this.name]['normal'] == 'string'){
                        popup_win(this.name, '<font color=#666>' + reg_msg[this.name]['normal'] + '</font>');
                    }
                }
            }
        }
    }

function tx_chk(obj){
    var val = obj.value;
    var cn = obj.name;

    var result = true;
    // check username
    if(cn == 'username'){
        if(strLen(val) == 0 || (strLen(val) < 4 || strLen(val) > 16)){
            popup_win(obj.name, "长度必须为4-16个字符");
            result = false;
        }

        if((/[^A-Za-z0-9]/).test(val)){
            popup_win(obj.name, "只能为英文和数字");
            result = false;
        }
    }

    //check password
    if(cn == 'password'){
        if(strLen(val) == 0 || (strLen(val) < 4 || strLen(val) > 24)){
            popup_win(obj.name, "长度必须为4-24个字符");
            result = false;
        }

        if((/[^A-Za-z0-9_]/).test(val)){
            popup_win(obj.name, "只能包含英文，数字和_");
            result = false;
        }
    }

    if(cn == 'password2'){
       if(oo('password').value != val){
           popup_win(obj.name, "两次输入的密码不相同");
           result = false;
       }
    }
    return result;
}

function cancel_popup_win(idName, prompt){
    if(oo('msg_'+idName)){
        oo('msg_'+idName).parentNode.removeChild(oo('msg_'+idName));
    }

    if(!oo('pic_right_'+idName) && prompt == true){
        var img_obj = document.createElement('img');
        img_obj.id = 'pic_right_'+idName;
        img_obj.className = 'pass';
        img_obj.src = "/media/register/img/d_bg.gif";
        oo(idName).parentNode.parentNode.appendChild(img_obj);
    }
}

function oo(obj){
    return document.getElementById(obj);
}

/**
 * 创建冒泡窗口
 */
function popup_win(idName, msg){
    var str = '';
    str += '<img src = "/media/register/img/tsbg1.jpg" class = "tsgb1" />';
    str += '<span class = "ts_bg">' + msg + '</span>';
    str += '<img src = "/media/register/img/tsbg2.jpg" class = "tsbg2" />';

    // create one div
    var div_obj = document.createElement('div');
    div_obj.className = 'ts';
    div_obj.id = 'msg_' + idName;
    div_obj.style.display = 'none';
    if(oo('pic_right_' + idName)){
        oo('pic_right_' + idName).parentNode.removeChild(oo('pic_right_' + idName));
    }

    if(!oo(div_obj.id)){
        oo(idName).parentNode.parentNode.appendChild(div_obj);
        div_obj.innerHTML = str;
        div_obj.style.display = 'block';
    }else{
        oo(div_obj.id).innerHTML = str;
    }
}

function fm_chk(fm){
    var i;
    var result = true;
    for(i = 0; i < fm.length; ++i){
        var obj = fm[i];
        result = tx_chk(obj) && result;
    }

    return result;
}

function strLen(str){
    return str.trim().length;
}